//! Kafka配置管理
//! 
//! 定义Kafka连接和行为配置

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Kafka配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KafkaConfig {
    /// Kafka broker地址列表
    pub brokers: Vec<String>,
    /// 安全配置
    pub security: SecurityConfig,
    /// 生产者配置
    pub producer: ProducerConfig,
    /// 消费者配置
    pub consumer: ConsumerConfig,
    /// 主题配置
    pub topics: TopicConfig,
}

/// 安全配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SecurityConfig {
    /// 安全协议 (PLAINTEXT, SSL, SASL_PLAINTEXT, SASL_SSL)
    pub protocol: String,
    /// SASL机制 (PLAIN, SCRAM-SHA-256, SCRAM-SHA-512)
    pub sasl_mechanism: Option<String>,
    /// SASL用户名
    pub sasl_username: Option<String>,
    /// SASL密码
    pub sasl_password: Option<String>,
    /// SSL证书路径
    pub ssl_ca_location: Option<String>,
    /// SSL客户端证书路径
    pub ssl_certificate_location: Option<String>,
    /// SSL客户端密钥路径
    pub ssl_key_location: Option<String>,
}

/// 生产者配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProducerConfig {
    /// 确认模式 (0, 1, all)
    pub acks: String,
    /// 重试次数
    pub retries: u32,
    /// 批量大小
    pub batch_size: u32,
    /// 压缩类型 (none, gzip, snappy, lz4, zstd)
    pub compression_type: String,
    /// 请求超时时间（毫秒）
    pub request_timeout_ms: u32,
    /// 其他配置项
    pub additional_properties: HashMap<String, String>,
}

/// 消费者配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConsumerConfig {
    /// 消费者组ID
    pub group_id: String,
    /// 自动偏移量重置策略 (earliest, latest, none)
    pub auto_offset_reset: String,
    /// 是否启用自动提交
    pub enable_auto_commit: bool,
    /// 自动提交间隔（毫秒）
    pub auto_commit_interval_ms: u32,
    /// 会话超时时间（毫秒）
    pub session_timeout_ms: u32,
    /// 最大轮询记录数
    pub max_poll_records: u32,
    /// 其他配置项
    pub additional_properties: HashMap<String, String>,
}

/// 主题配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TopicConfig {
    /// 沙箱事件主题
    pub sandbox_events: String,
    /// 执行任务主题
    pub execution_tasks: String,
    /// 系统监控主题
    pub system_metrics: String,
    /// 错误报告主题
    pub error_reports: String,
    /// 健康检查主题
    pub health_checks: String,
}

impl Default for KafkaConfig {
    fn default() -> Self {
        Self {
            brokers: vec!["localhost:9092".to_string()],
            security: SecurityConfig::default(),
            producer: ProducerConfig::default(),
            consumer: ConsumerConfig::default(),
            topics: TopicConfig::default(),
        }
    }
}

impl Default for SecurityConfig {
    fn default() -> Self {
        Self {
            protocol: "PLAINTEXT".to_string(),
            sasl_mechanism: None,
            sasl_username: None,
            sasl_password: None,
            ssl_ca_location: None,
            ssl_certificate_location: None,
            ssl_key_location: None,
        }
    }
}

impl Default for ProducerConfig {
    fn default() -> Self {
        Self {
            acks: "all".to_string(),
            retries: 3,
            batch_size: 16384,
            compression_type: "snappy".to_string(),
            request_timeout_ms: 30000,
            additional_properties: HashMap::new(),
        }
    }
}

impl Default for ConsumerConfig {
    fn default() -> Self {
        let mut additional_properties = HashMap::new();
        additional_properties.insert("heartbeat.interval.ms".to_string(), "3000".to_string());
        additional_properties.insert("max.poll.interval.ms".to_string(), "300000".to_string());
        
        Self {
            group_id: "koda-sandbox-manager".to_string(),
            auto_offset_reset: "latest".to_string(),
            enable_auto_commit: true,
            auto_commit_interval_ms: 5000,
            session_timeout_ms: 30000,
            max_poll_records: 500,
            additional_properties,
        }
    }
}

impl Default for TopicConfig {
    fn default() -> Self {
        Self {
            sandbox_events: "koda.sandbox.events".to_string(),
            execution_tasks: "koda.execution.tasks".to_string(),
            system_metrics: "koda.system.metrics".to_string(),
            error_reports: "koda.error.reports".to_string(),
            health_checks: "koda.health.checks".to_string(),
        }
    }
}